using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TournamentLayer : MonoBehaviour
{
	public GameObject tournamentButtonPrefab;

	public UITable table;

	public UILabel titleLabel;

	public UISprite flagSprite;

	protected List<TournamentStageButton> stageButtons = new List<TournamentStageButton>();

	protected PlayerCareerStageDefinition.StageDefinition stage;

	public void Show(PlayerCareerStageDefinition.StageDefinition stage)
	{
		this.stage = stage;
		NavigationManager.instance.Push(base.gameObject);
	}

	private void OnEnable()
	{
		Init(stage);
	}

	public void Refresh()
	{
		Init(stage);
	}

	private void Init(PlayerCareerStageDefinition.StageDefinition stage)
	{
		int num = 0;
		GameConstants.SetFlag(flagSprite, stage.flag);
		if (stage.stageProgress.isLost || stage.stageProgress.stage.wonGames >= stage.subStages.Count)
		{
			stage.stageProgress.Reset();
		}
		stage.stageProgress.MakeSane();
		foreach (TournamentStageButton stageButton in stageButtons)
		{
			stageButton.gameObject.SetActive(false);
		}
		PlayerCareerStageDefinition.StageDefinition.SubStageDefinition subStageDefinition = null;
		PlayerCareer.StageProgress stageProgress = stage.stageProgress;
		foreach (PlayerCareerStageDefinition.StageDefinition.SubStageDefinition subStage in stage.subStages)
		{
			if (num >= stageButtons.Count)
			{
				TournamentStageButton component = NGUITools.AddChild(table.gameObject, tournamentButtonPrefab).GetComponent<TournamentStageButton>();
				stageButtons.Add(component);
			}
			if (num == stageProgress.stage.wonGames)
			{
				subStageDefinition = subStage;
			}
			TournamentStageButton tournamentStageButton = stageButtons[num];
			tournamentStageButton.gameObject.name = GGFormat.IndexName(num);
			tournamentStageButton.gameObject.SetActive(true);
			tournamentStageButton.Init(this, stage, num);
			num++;
		}
		GameConstants.SetFlag(flagSprite, stage.flag);
		if (subStageDefinition != null)
		{
			titleLabel.text = stage.name + " - " + subStageDefinition.name;
		}
		else
		{
			titleLabel.text = stage.name;
		}
		table.Reposition();
		ScrollToInitial();
	}

	private void ScrollToInitial()
	{
		StopAllCoroutines();
		StartCoroutine(DoScrollToInitial());
	}

	private IEnumerator DoScrollToInitial()
	{
		for (int i = 0; i < 4; i++)
		{
			UITools.AlignToLeftOnScroll(stageButtons[0].GetComponent<UIWidget>(), table.transform.parent.GetComponent<UIScrollView>(), 10f);
			yield return null;
		}
	}
}
